package com.sfx.concurrent.demo.concurrent.AlternatingOutput;

import java.util.concurrent.locks.LockSupport;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-14
 * Time: 12:04
 */

public class demo3 {
    private static Thread t1;
    private static Thread t2;
    private static Thread t3;

    public static void main(String[] args) throws InterruptedException {
        ParkUnPark parkUnPark = new ParkUnPark(5);
        t1 = new Thread(()->{
            parkUnPark.print("a",t2);
        },"t1");

        t2 = new Thread(()->{
            parkUnPark.print("b",t3);
        },"t2");

        t3 = new Thread(()->{
            parkUnPark.print("c",t1);
        },"t3");
        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);
    }
}

class ParkUnPark{
    private int loopNumber;

    public ParkUnPark(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void print(String str,Thread next) {
        for(int i =0;i<loopNumber;++i) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }
    }
}